package com.hibernate.hql;

import java.util.List;
import java.util.Set;

import org.hibernate.classic.Session;
import org.junit.Test;

import com.hibernate.domain.Course;
import com.hibernate.domain.Student;
import com.hibernate.utils.HibernateUtil;

/**
 * hibernate之hql多对多关系查询测试类
 * @author LC
 *
 */
public class ManyToManyQueryTest extends HibernateUtil {
	
	/**
	 * hibernate中无法进行多对多关系的等值连接
	 */
	
	/**
	 * 迫切内连接
	 */
	@Test
	public void testQuery_inner_join_fetch() {
		Session session = sessionFactory.openSession() ;
		String hql = "from Student s inner join fetch s.courses c" ;
		List<Student> list = session.createQuery(hql).list() ;
		for (Student student : list) {
			System.out.println(student);
			Set<Course> courses = student.getCourses() ;
			for (Course course : courses) {
				System.out.println(course);
			}
		}
	}
	
	/**
	 * 迫切左外连接
	 */
	@Test
	public void testQuery_left_outer_join_fetch() {
		Session session = sessionFactory.openSession() ;
		String hql = "from Student s left outer join fetch s.courses c" ;
		List<Student> list = session.createQuery(hql).list() ;
		for (Student student : list) {
			System.out.println(student);
			Set<Course> courses = student.getCourses() ;
			for (Course course : courses) {
				System.out.println(course);
			}
		}
	}
	
	

}
